package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"user-srv/pkg"
)

func JWTPare() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")

		if token == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token不能为空",
			})
			c.Abort()
			return
		}
		userId, err := pkg.GetToken(token)
		if err != "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token解析失败",
			})

		}
		c.Set("userId", userId)
		c.Next()

	}
}
